limsOutsourceApply.resultPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'lims-outsource-apply-result-list-grid',
            url: limsOutsourceApply.baseUrl + '/queries/tasks',
            columns: limsOutsourceApply.getResultListColumns(),
            onCellClick: function (field, row) {
                field === 'applyCode' && _this.loadDetailPage(row.id)
            },
            page: true,
            requestData: {
                // status_EQ: 'logged',
                bpmn_statusCode: 'result'
            },
            toolbar: [/*{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.resultSubmit()
                }
            }*/{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                icon: 'pass',
                onClick: function () {
                    _this.pass()
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                icon: 'reject',
                onClick: function () {
                    _this.reject()
                }
            },]
        }
    },

    pass: function () {
        var grid = Gikam.getComp('lims-outsource-apply-result-list-grid')
        Gikam.create('workflow').pass({
            data: grid.getSelections(),
            pageObject: limsOutsourceApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    reject: function () {
        var grid = Gikam.getComp('lims-outsource-apply-result-list-grid')
        Gikam.create('workflow').reject({
            data: grid.getSelections(),
            pageObject: limsOutsourceApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    resultSubmit: function () {
        var grid = Gikam.getComp('lims-outsource-apply-result-list-grid')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.WORKFLOW.BUTTON.SUBMIT', function () {
            workspace.window.showMask()
            Gikam.postText(limsOutsourceApply.baseUrl + "/result-done", Gikam.getJsonWrapper({}, [null, selections])).done(function (data) {
                grid.refresh()
            }).always(function () {
                workspace.window.closeMask()
            })
        })
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-outsource-apply-result-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        })
    },

    loadDetailPage: function (id) {
        workspace.window.load(limsOutsourceApply.baseUrl + '/page/detail' + Gikam.param({
            id: id,
            step: 'result'
        }))
    },

    init: function () {
        this.create()
    }
}
